package com.ablife.limiter.service;

import java.io.IOException;
import javax.annotation.PostConstruct;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Service
public class InitService {
    private static final Logger logger = LoggerFactory.getLogger(InitService.class);
    @Autowired
    JedisPool jedisPool;

    public InitService() {
    }

    @PostConstruct
    public void initRedis() throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
        Jedis jedis = this.jedisPool.getResource();
        logger.info("begin initialize redis config---------------------");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(this.getClass().getClassLoader().getResource("initconfig.xml").getPath());
        XPathFactory xFactoty = XPathFactory.newInstance();
        XPath xPath = xFactoty.newXPath();
        XPathExpression compile = xPath.compile("//ConfigGroup");
        NodeList nodeList = (NodeList)compile.evaluate(doc, XPathConstants.NODESET);
        String group = "";
        String sum = "";
        String timeout = "";

        int i;
        for(i = 0; i < nodeList.getLength(); ++i) {
            NodeList attr = nodeList.item(i).getChildNodes();

            for(i = 0; i < attr.getLength(); ++i) {
                Node node = attr.item(i);
                if ("GroupID".equals(node.getNodeName())) {
                    group = node.getTextContent();
                }

                if ("GroupLimit_Count".equals(node.getNodeName())) {
                    sum = node.getTextContent();
                }

                if ("GroupLimit_Time".equals(node.getNodeName())) {
                    timeout = node.getTextContent();
                }
            }

            logger.info("group=" + group + ",sum=" + sum + ",timeout=" + timeout);
            if (!jedis.exists("CONFIG|" + group.toUpperCase())) {
                jedis.set("CONFIG|" + group.toUpperCase(), "B|" + sum + "|T|" + timeout);
                jedis.set("TRADECOUNT|" + group.toUpperCase(), "0");
            }
        }

        compile = xPath.compile("//ConfigTrade");
        nodeList = (NodeList)compile.evaluate(doc, XPathConstants.NODESET);
        String trade = "";
        String channel = "";

        for(i = 0; i < nodeList.getLength(); ++i) {
            NodeList attr = nodeList.item(i).getChildNodes();

            for(int j = 0; j < attr.getLength(); ++j) {
                Node node = attr.item(j);
                if ("Trade2GroupID".equals(node.getNodeName())) {
                    group = node.getTextContent();
                }

                if ("Trade".equals(node.getNodeName())) {
                    trade = node.getTextContent();
                }

                if ("Channel".equals(node.getNodeName())) {
                    channel = node.getTextContent();
                }
            }

            logger.info("group=" + group + ",trade=" + trade + ",channel=" + channel);
            if (!jedis.exists("TRADECONFIG|" + trade.toUpperCase() + "|" + channel.toUpperCase())) {
                jedis.set("TRADECONFIG|" + trade.toUpperCase() + "|" + channel.toUpperCase(), group);
            }
        }

        if (!jedis.exists("MAINSWITCH")) {
            jedis.set("MAINSWITCH", "0");
        }

        jedis.close();
        logger.info("end------------------------------------");
    }
}
